package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.annotation.AutoFill;
import com.sky.constant.StatusConstant;
import com.sky.context.BaseContext;
import com.sky.dto.CategoryDTO;
import com.sky.dto.CategoryPageQueryDTO;
import com.sky.entity.Category;
import com.sky.entity.Dish;
import com.sky.entity.Setmeal;
import com.sky.enumeration.OperationType;
import com.sky.mapper.CategoryMapper;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.result.PageBean;
import com.sky.service.CategoryService;
import com.sky.service.SetmealService;
import com.sky.vo.DishVO;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.logging.Log;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

/**
 * 分类业务层
 */
@Service
@Slf4j
public class CategoryServiceImpl implements CategoryService {
    @Autowired
    CategoryMapper categoryMapper;
    @Autowired
    DishMapper dishMapper;
    @Autowired
    SetmealMapper setmealMapper;

    /**
     * 新增一个菜品分类
     *
     * @param categoryDTO
     */
    @Override
    public void addOneCategory(CategoryDTO categoryDTO) {
        Category category = new Category();
        //对象复制
        BeanUtils.copyProperties(categoryDTO, category);
        // 补 全对象
//        category.setCreateTime(LocalDateTime.now());
//        category.setCreateUser(BaseContext.getCurrentId());
        category.setStatus(StatusConstant.ENABLE);
//        category.setUpdateTime(LocalDateTime.now());
//        category.setUpdateUser(BaseContext.getCurrentId());
        categoryMapper.add(category);

    }

    /**
     * 分页查询菜品分类
     *
     * @param categoryPageQueryDTO
     * @return
     */
    @Override
    public PageBean selectByPage(CategoryPageQueryDTO categoryPageQueryDTO) {
        PageHelper.startPage(categoryPageQueryDTO.getPage(), categoryPageQueryDTO.getPageSize());
        Page<Category> page = categoryMapper.selectByPage(categoryPageQueryDTO);
        return new PageBean(page.getTotal(), page.getResult());

    }

    /**
     * 删除菜品分类
     *
     * @param id
     */
    @Override
    public void delete(Long id) {
        List<String> listByCategoryId = dishMapper.listNameByCategorysId(id);
        List<Setmeal> setmeals = setmealMapper.getSetmealByCategoryId(id);
        log.info("============关联菜品名字{}==============", listByCategoryId);
        log.info("============关联套餐名字{}==============", setmeals);
        if (listByCategoryId == null) {
            throw new RuntimeException("分类关联菜品，禁止删除，请先更改菜品分类");
        }
        if (setmeals == null) {
            throw new RuntimeException("分类关联套餐，禁止删除！请先更改套餐分类");
        }

        categoryMapper.deleteById(id);
    }

    /**
     * 修改分类
     *
     * @param categoryDTO
     */
    @Override
    public void update(CategoryDTO categoryDTO) {
        Category category = new Category();
        //设置更新时间和更新人
        BeanUtils.copyProperties(categoryDTO, category);
//        category.setUpdateUser(BaseContext.getCurrentId());
//        category.setUpdateTime(LocalDateTime.now());
        categoryMapper.update(category);
    }

    /**
     * 启用禁用分类
     *
     * @param status
     * @param id
     */
    @Override
    public void changeStatus(Integer status, Long id) {
        Category category = new Category();
        category.setUpdateTime(LocalDateTime.now());
        category.setId(id);
        category.setStatus(status);
        categoryMapper.update(category);
    }

    /**
     * 根据类型查询分类
     *
     * @param type
     * @return
     */
    @Override
    public List<Category> listByType(Integer type) {
        List<Category> categories = categoryMapper.listByType(type);
        return categories;
    }

    /**
     * 根据分类查询
     *
     * @param type
     * @return
     */
    @Override
    public List<Category> list(Integer type) {
        List<Category> categories = categoryMapper.listByType(type);
        return categories;
    }
}
